﻿


CREATE TRIGGER [tr_WorkFlow_Insert]
ON [dbo].[t_WorkFlow]
FOR INSERT
AS

IF EXISTS(SELECT * FROM v_WorkFlow wf INNER JOIN inserted i
					ON i.MenuID = wf.MenuID AND i.NextMenuID = wf.NextMenuID AND
						 wf.StatusNameSpace <> wf.NextStatusNameSpace)BEGIN
	ROLLBACK TRAN
	RAISERROR('MenuFrom and MenuTo must be from the same namespace',16,1)
	RETURN
END

RETURN -->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

DECLARE @MenuID int, @NextMenuID int,@Text varchar(8000)

DECLARE cur CURSOR LOCAL FOR
SELECT MenuID,NextMenuID FROM inserted

OPEN cur

FETCH NEXT FROM cur INTO @MenuID,@NextMenuID

WHILE @@FETCH_STATUS = 0
BEGIN
	
	IF NOT EXISTS(SELECT * FROM t_WorkFlow_Groups WHERE MenuID=@MenuID AND NextMenuID=@NextMenuID AND GroupName='Admin')
		INSERT INTO t_WorkFlow_Groups SELECT @MenuID,@NextMenuID,'Admin'
	FETCH NEXT FROM cur INTO @MenuID,@NextMenuID
END

CLOSE cur
DEALLOCATE cur






